package org.example.code.mobile.popup.actions;


import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.example.code.mobile.wizard.WizardGenerateCode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class GenerateWizardMobilApplicationAction implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public GenerateWizardMobilApplicationAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
//		WizardDialog dialog = new WizardDialog(shell, new WizardGenerateCode());
//		dialog.open(); 
		 WizardDialog wizardDialog = new WizardDialog(shell,
			      new WizardGenerateCode());
			    if (wizardDialog.open() == Window.OK) {
			    	MessageDialog.openInformation(
			    			shell,
			    			"Mobile Application Developer",
			    			"Code Generated Succesfully.");
			      System.out.println("Ok pressed");
			    } else {
			    	MessageDialog.openInformation(
			    			shell,
			    			"Mobile Application Developer",
			    			"Code Generate Canceled.");
			    	
			       }

		try {

  		IWorkbenchWindow window = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow();

			ISelection selection = window.getSelectionService().getSelection(
					"org.eclipse.ui.navigator.ProjectExplorer");

			TreePath[] paths = ((TreeSelection) selection).getPaths();
			TreePath p = paths[0];
			File fXmlFile = new File((EFS.getLocalFileSystem()
					.getStore(((IFile) p.getLastSegment()).getLocation())).toURI());
			
//			File fXmlFile = (File) p.getLastSegment();
	//		((IFile) p.getLastSegment()).getLocation()
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			//optional, but recommended
			//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
			doc.getDocumentElement().normalize();
		 
			//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			 
			//NodeList nList = doc.getElementsByTagName("components");
		 
			NodeList hijos = doc.getChildNodes();
			
			for (int temp = 0; temp < hijos.getLength(); temp++) {
				Node nNode = hijos.item(temp);
				System.out.print("\nCurrent Element :" + nNode.getNodeName() + " - ");
				
				NodeList hijos2 = nNode.getChildNodes();
				
				for (int temp2 = 0; temp2 < hijos2.getLength(); temp2++) {
					Node nNode2 = hijos2.item(temp2);
					if (!nNode2.getNodeName().contains("text")){
						System.out.print("\nCurrent Element :" + nNode2.getNodeName() + " - ");						
					}
					NodeList hijos3 = nNode.getChildNodes();
					
					for (int temp3 = 0; temp3 < hijos3.getLength(); temp3++) {
						Node nNode3 = hijos3.item(temp3);
						if (!nNode3.getNodeName().contains("text")){
							System.out.print("\nCurrent Element :" + nNode3.getNodeName() + " - ");						
						}
					}
				}

			}
			
			System.out.println("----------------------------");
//
//			//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
//			for (int temp = 0; temp < nList.getLength(); temp++) {
//				 
//				Node nNode = nList.item(temp);
//		 
//				System.out.print("\nCurrent Element :" + nNode.getNodeName() + " - ");
//		 
//				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//		 
//					Element eElement = (Element) nNode;
//		 
//					System.out.print("components : " + eElement.getAttribute("xsi:type"));		 
//				}
//			}

			
		} catch (Exception blah) {
			System.out.println(blah);
		}


		MessageDialog.openInformation(shell, "Mobile Application Developer",
				"Generate Mobil Application.");
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
